package com.white.commonlib.adapter.listgrid;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.List;

import butterknife.ButterKnife;

/**
 * Created by ytf on 2016/08/10.
 * Description: 默认listview、gradiview的适配器
 * Modify by xx on 2016/08/10.
 * Modify detail:
 */
public abstract class DefaultAdapter<D> extends BaseAdapter {

    public List<D> datas;
    public Context context;
    public static final int STATE_NORMAL = 1;
    public static final int STATE_MORE = 2;

    public DefaultAdapter(Context context, List<D> datas){
        this.datas = datas;
        this.context = context;
    }

    @Override
    public int getCount() {
        return datas == null ? 0 : datas.size();
    }

//    @Override
//    public int getViewTypeCount()
//    {
//        return 2;
//    }
//
//    @Override
//    public int getItemViewType(int position)
//    {
//        return position <= datas.size()-1 ? STATE_NORMAL : STATE_MORE;
//    }

    @Override
    public Object getItem(int position) {
        return datas == null ? null : datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        BaseViewHolder holder = null;
        if(convertView == null)
        {
            holder = getHolderInstance(context);
            ButterKnife.bind(holder, holder.getContentView());
        }else
        {
            holder = (BaseViewHolder) convertView.getTag();
        }
        //特殊的的地方在小说宫格显示，无数据的地方补全
        holder.setData(position < datas.size() ? datas.get(position) : null, position);

        return holder.getContentView();
    }

    protected abstract BaseViewHolder<D> getHolderInstance(Context context);

}
